package com.feihong.yw189.utils;

import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.feihong.yw189.global.GlobalConfig;

import org.json.JSONObject;
import org.xutils.common.util.LogUtil;

import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by PVer on 2018/1/5.
 */

public class ToolsUtil {
    public static final String TAG = "营维189";

    /**
     * json排序
     * @param jsonStr json格式的字符串
     * @return String类型
     * @param map 想额外添加参数, map格式添加
     * @return
     * @throws Exception
     */
    public static TreeMap jsonSort(String jsonStr, Map<String, String> map) throws Exception {

        TreeMap<String,String> treeMap = new TreeMap<String, String>();

        //如果map不为空,遍历map
        if(map != null && !map.isEmpty()){
            for (Map.Entry<String, String> entry : map.entrySet()) {
                //sign 和token 不参与加密
                if(! entry.getKey().equals("sign") && ! entry.getKey().equals("token")){
                    treeMap.put(entry.getKey(), entry.getValue());
                }
            }
        }

        if(jsonStr != null && !jsonStr.isEmpty()){
            JSONObject json = new JSONObject(jsonStr);
            Iterator<String> keyItem = json.keys();
            String keyName = "";
            while(keyItem.hasNext()){
                keyName = keyItem.next();
                treeMap.put(keyName, ""+json.getString(keyName));
            }
        }

        return treeMap;
    }

    /**
     * 得到签名sign
     * @param jsonStr
     * @return 签名后的sign
     * @throws Exception
     */
    public static String getsignRes(String jsonStr, Map<String, String> map){

       try{
           TreeMap<String, String> treeMap = jsonSort(jsonStr, map);
           LogUtil.i(TAG + "---:" + treeMap);

           StringBuilder result = new StringBuilder();

           // 取键值
           for(Map.Entry<String, String> entry: treeMap.entrySet()) {
               result.append(entry.getValue());
           }
           LogUtils.i(TAG + "encode:-------- " + result);

           result.append(GlobalConfig.PUBLIC_KEY);
           String sign = encode(result.toString());
            Log.e("项目异常", "------getsignRes------" + result.toString());
           return ""+ sign;
       }catch (Exception e){
           return "哎呀,出错了!";
       }
    }

    /**
     * 加密
     * @param string
     * @return
     */
    public static String encode(String string){
        try{
            byte[] hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
            StringBuilder hex = new StringBuilder(hash.length * 2);
            for (byte b : hash) {
                if ((b & 0xFF) < 0x10) {
                    hex.append("0");
                }
                hex.append(Integer.toHexString(b & 0xFF));
            }

            return hex.toString();
        }catch (Exception e){
            LogUtils.e(TAG + "encode---err: " + e);
            return "encode---err";
        }
    }
}
